/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
package org.apache.directory.mavibot.btree;

import java.lang.management.ManagementFactory;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;

/**
 * This class create a MBean for a Btree and register it on the MBean server.
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
public class JMXMavibotFactory
{
    /** The index use to differentiate btrees. */
    private static long index = 0;
    
    /**
     * 
     * Create a MBean for a Btree and register it on the MBean server.
     *
     * @param btree The btree to manage with a MBean.
     */
    public static <TK,TV> void createJMXBean(AbstractBTree<TK, TV> btree)
    {
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        ObjectName name = null;   
        
        try
        {
            if(btree.getType() == BTreeTypeEnum.PERSISTED)
            {
                JMXMavibotPersistedBtree<TK, TV> bean = new JMXMavibotPersistedBtree<TK, TV>(btree);
                name = new ObjectName( "org.apache.directory.mavibot.btree:type=JMXMavibotPersistedBtree,name="+(index++)+"_"+btree.getName() );
                mBeanServer.registerMBean( bean, name );
            }
            else
            {
                JMXMavibotInMemoryBtree<TK, TV> bean = new JMXMavibotInMemoryBtree<TK, TV>(btree);
                name = new ObjectName( "org.apache.directory.mavibot.btree:type=JMXMavibotInMemoryBtree,name="+(index++)+"_"+btree.getName() );
                mBeanServer.registerMBean( bean, name );
            }
        }
        catch ( InstanceAlreadyExistsException e )
        {e.printStackTrace();}
        catch ( MBeanRegistrationException e )
        {e.printStackTrace();}
        catch ( NotCompliantMBeanException e )
        {e.printStackTrace();}
        catch ( MalformedObjectNameException e )
        {e.printStackTrace();}
        
    }
}
